
<template>
  <view class="order">
    <!-- <hunk-head name="马上安家"></hunk-head> -->
    <div class="banner">
      <img :src="banner" alt="" srcset="">
    </div>
    <scroll-view
      class="prv-container"
      scroll-y="true"
      @scrolltolower="_onLoadmore"
    >
      <div class="order-list" v-if="orderList[0].wid">
        <div class="list-item" v-for="(item, index) in orderList" :key="index" @click="detail(item)">
          <div class="leftImg">
            <img :src="item.defaultimg" alt="" srcset="">
          </div>
          <div class="rightText">
            <div class="text-top">
              {{item.outtype ? '整租':'合租'}}-{{item.communityname}}
            </div>
            <div class="time">
              <span>租约开始时间：</span>
              {{item.starttime}}
            </div>
            <div class="time">
              <span>租约结束时间：</span>
              {{item.endtime}}
            </div>
            <div class="time" v-if="item.withdrawaltime">
              <span>{{item.state==2?'已结束时间：':item.state==3?'提前结束时间：':''}} </span>
              {{item.withdrawaltime}}                                        
            </div>
            <div class="btn">
              <div :class="['status',item.state == 3 ? 'width76': '']">
                <img v-if="item.state!=4" :src="imgs[item.state-1]" alt="" srcset="">
                <div v-else> 强制退租</div>
              </div>
              <div class="tuizu" v-if="item.state == 1" @click.stop="enforceDetail(item)">强制退租</div>
            </div>
          </div>
        </div>
      </div>
    </scroll-view>
    <uni-popup ref="popup" type="center">
      <div class="tips">
        <div class="close" @click="closePopup">
          <img v-if="baseUrl" :src="baseUrl+'order/close.png'" alt="" srcset="">
        </div>
        <div class="tips-top">{{popupList.communityname}}</div>
        <div class="tishi">
          <img v-if="baseUrl" :src="baseUrl+'order/tips.png'" alt="">
          <div>退租后租客无法开门</div>
        </div>
        <div class="tips-text">
          <div class="text-item">
            <div class="left">房源月租：</div>
            <div class="right">
              {{popupList.rent}}
              <span>/月</span>
            </div>
          </div>
          <div class="text-item">
            <div class="left">租客姓名：</div>
            <div class="right">
              {{popupList.unickname}}
            </div>
          </div>
          <div class="text-item">
            <div class="left">租客身份证：</div>
            <div class="right">
              {{popupList.cardid}}
            </div>
          </div>
          <div class="text-item">
            <div class="left">原本的租约结束时间：</div>
            <div class="right red">
              {{popupList.endtime}}
            </div>
          </div>
        </div>
        <div class="tips-btn">
          <button :disabled="isdisabled" :class="[isdisabled?'':'green']" @click="confirm(popupList.tenantuserid,popupList.wid)">
            {{times}}
            <span v-show="times !== '确认'">s</span>
          </button>
        </div>
      </div>
    </uni-popup>
    <div v-if="isnone < 1" class="none">
      <img v-if="baseUrl" :src="baseUrl+'imgs/imgs/none.png'" alt="">
    </div>
  </view>
</template>

<script>
const imgUrl = `${process.env.VUE_APP_BASE_IMG_URL}`
import {debounce} from 'lodash'
import Head from '@componentsMine/head'
const banner = imgUrl+'order/banner.png'
const over = imgUrl+'order/over.png'
const overed = imgUrl+'order/overed.png'
const zuing = imgUrl+'order/zuing.png'
import { getHouseContractOrder,forceRetreatRent } from '@/apis/order'
export default {
  data() {
    return {
      banner,
      imgs:[
        zuing,
        over,
        overed
      ],
      title: 'Hello',
      times: 5,
      timer: null,
      listInfo: {
        limit: '10',
        offset: '1',
        type: "1"
      },
      isdisabled: true,
      orderList: [
        {
          "cardid": "",
          "communityname": "wblv版里v花无百日红v；我让v白皮 ",
          "defaultimg": "",
          "endtime": "",
          "outtype": 0,
          "rent": 0,
          "starttime": "",
          "state": 1,   //1在租中，2已结束，3提前结束，4强制退租
          "tenantuserid": 0, //租客userid
          "unickname": "",  //租客名称
          "wid": 0
        }
      ],
      popupList: {
        "cardid": "341622199301105990",
        "communityname": "天星美和园公寓3栋-二单元-302",
        "defaultimg": "",
        "endtime": "2022/12/30",
        "outtype": 0,
        "rent": 1200,
        "starttime": "2021/12/30",
        "state": 1,   //1在租中，2已结束，3提前结束，4强制退租
        "tenantuserid": 0, //租客userid
        "unickname": "",  //租客名称
        "wid": 0
      }
    }
  },
  watch: {
    times(val,nel){
      if(val == 0){
        let that = this
        clearInterval(that.timer )
        this.timer = null
        this.times = '确认'
        this.isdisabled = false
        console.log('val=0 ',this.isdisabled,this.times )
      }else if(val != '确认'){
        console.log('val11 ',val,this.isdisabled,this.times)
        this.isdisabled = true
      }
    }
  },
  components: {
    'hunk-head': Head
  },
  created(){
    this.listInfo.offset = 1
    this.getHouseContractOrder()
    // console.log(12222,getCurrentPages())
  },
  computed: {
    userid(){
      return this.$store.state.baseStore.userInfo.userid
    },
    userInfo(){
      return this.$store.state.baseStore.userInfo
    },
    baseUrl() {
      return process.env.VUE_APP_BASE_IMG_URL;
    },
    isnone(){
      if(this.orderList){
        return this.orderList.length
       }else{
         return false
       }
    }
  },
  methods: {
    detail(item){
      this.$jump({ path: '/pages/tenant/lease/detaillease?query=' + JSON.stringify(item) })
    },
    getHouseContractOrder(){
      //业主
      let that = this
      if(this.userInfo.usertype == 2 ){
        this.listInfo.type = 0 + ""
      }else if(this.userInfo.usertype == 1){ //管理员
        this.listInfo.type = 1+""
      }
      getHouseContractOrder({...this.listInfo,userid:this.userid}).then(res=>{
        if(res.code == 200 ){
          that.orderList = res.data
          that.$forceUpdate()
        }else{
          uni.showToast({
            title: res.msg,
            icon: 'none'
          })
        }
      })
    },
    enforceDetail(item){
      this.popupList = item
      this.$refs.popup.open()
      this.changeTimes()
    },
    changeTimes(){
      this.times = 5
      this.timer = setInterval(()=>{
        this.times--
      },1000)
    },
    _onLoadmore(){
      ++this.listInfo.offset
      this.getHouseContractOrder()
      console.log('_onLoadmore')
    },
    closePopup(){
      this.$refs.popup.close()
      clearInterval(this.timer)
      this.timer = null
      this.times = 5
    },
    confirm:debounce(function(uid,wid){
      let that = this
      // console.log('99999',this.popupList,uid)
      const data = {
        tenantuserid:uid,
        userid:this.userid,
        wid:wid
      }
      forceRetreatRent(data).then(res=>{
        if(res.code == 200){
          // this.orderList = res.data
          uni.showToast({
            title: res.msg,
            icon: 'none',
            success: ()=>{
              that.listInfo.offset = 1
              that.$refs.popup.close()
              that.getHouseContractOrder()
            }
          })
        }else{
          uni.showToast({
            title: res.msg,
            icon: 'none'
          })
        }
      })
    },1000)
  },
  mounted() {
  }
}
</script>

<style lang="scss" scoped>
.order{
  @include num();
  display: flex;
  flex-direction: column;
  .banner{
    width: 375px;
    height: 120px;
    img{
      width: 100%;
      height: 100%;
    }
  }
  .prv-container{
    flex: 1;
    overflow-y: auto;
  }
  .order-list{
    background: #fff;
    .list-item{
      box-sizing: border-box;
      padding: 15px;
      border-bottom: 2px solid #EFEFEF;
      display: flex;
      flex-direction: row;
    }
    .leftImg{
      width: 110px;
      height: 115px;
      border-radius: 4px;
      margin-right: 15px;
      img{
        width: 100%;
        height: 100%;
        border-radius: 4px;
      }
    }
    .rightText{
      flex: 1;
      .text-top{
        // height: 44px;
        font-size: 16px;
        font-family: PingFangSC-Medium, PingFang SC;
        font-weight: 500;
        color: #000000;
        line-height: 22px;
      }
      .time{
        margin-top: 4px;
        font-size: 14px;
        font-family: PingFangSC-Regular, PingFang SC;
        font-weight: 400;
        color: #4A4A4A;
        line-height: 20px;
        span{
          color: #9B9B9B;
        }
      }
      .btn{
        margin-top: 10px;
        display: flex;
        justify-content: space-between;
        .status{
          width: 62px;
          height: 20px;
          border-radius: 4px;
          img{
            width: 100%;
            height: 100%;
          }
          div{
            width: 100%;

            padding: 0 5px;
            height: 24px;
            box-sizing: border-box;
            // background: #EF1E13;
            background: rgba($color: #EF1E13, $alpha: 0.2);
            border-radius: 4px;
            font-size: 13px;
            font-family: PingFangSC-Regular, PingFang SC;
            font-weight: 400;
            color: #EF1E13;
            line-height: 24px;
            text-align: center;
          }
        }
        .width76{
         width: 76px; 
        }
        .tuizu{
          padding: 0 14px;
          height: 24px;
          background: #FF6B75;
          border-radius: 12px;
          font-size: 13px;
          font-family: PingFangSC-Medium, PingFang SC;
          font-weight: 500;
          color: #FFFFFF;
          line-height: 24px;
          text-align: center;
        }
      }
    }
  }
  .tips{
    width: 335px;
    height: 304px;
    background: #FFFFFF;
  }
  .close{
    position: absolute;
    top: 22px;
    right: 13px;
    width: 18px;
    height: 18px;
    img{
      width: 100%;
      height: 100%;
    }
  }
  .tips-top{
    padding: 0 34px;
    height: 60px;
    font-size: 18px;
    font-family: PingFangSC-Medium, PingFang SC;
    font-weight: 500;
    color: #4A4A4A;
    line-height: 60px;
    text-align: center;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
  }
  .tishi{
    height: 37px;
    background: #FF6B75;
    font-size: 13px;
    font-family: PingFangSC-Medium, PingFang SC;
    font-weight: 500;
    color: #FFFFFF;
    line-height: 37px;
    padding-left: 18px;
    display: flex;
    align-items: center;
    img{
      width: 16px;
      height: 16px;
      margin-right: 13px;
    }
  }
 .tips-text{
    box-sizing: border-box;
    padding-left: 15px;
    padding-top: 10px;
    .text-item{
      font-size: 14px;
      font-family: PingFangSC-Regular, PingFang SC;
      font-weight: 400;
      color: #9B9B9B;
      line-height: 20px;
      display: flex;
      margin: 5px 0;
      .left{
        margin-right: 12px;
      }
      .right{
        font-size: 14px;
        font-family: PingFangSC-Semibold, PingFang SC;
        font-weight: 600;
        color: #4A4A4A;
        line-height: 20px;
        span{
          font-size: 10px;
          color: #9B9B9B;
        }
      }
      .red{
        color: #FF6B75;
      }
    }
  }
  .tips-btn{
      width: 100%;
      display: flex;
      justify-content: center;
      align-items: center;
      margin-top: 15px;
      button{
        width: 305px;
        height: 45px;
        background: #CCCCCC;
        border-radius: 100px;
        font-size: 16px;
        font-family: PingFangSC-Medium, PingFang SC;
        font-weight: 500;
        color: #FFFFFF;
        line-height: 45px;
      }
      button::after{
        border: 0;
      }
      .green{
        background: #00CF64;
      }
    }
  .none{
    width: 100vw;
    margin-top: 50px;
    // background: #fff;
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;
    img{
      width: 190px;
      height: 195px;
    }
  }
}


</style>
